//
//  WelComeViewControl.swift
//  SinaWeibo
//
//  Created by 张子国 on 15/11/12.
//  Copyright © 2015年 张子国. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
class WelComeViewControl: UIViewController {
    override func loadView() {
        view  = backImage
    }
    
    //不能再次调用动画效果
    override func viewDidLoad() {
        super.viewDidLoad()
        setUpUI()
    }
    
    //视图布局完成后调用动画
    override func viewDidAppear(animated: Bool) {
        startAnimation()
    }
    
    
    private func startAnimation() {
    
    //改变外部约束
        iconImage.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(self.view.snp_bottom).offset(200 - self.view.bounds.height)
        }
        
        //动画效果
       UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: { () -> Void in
        self.view.layoutIfNeeded()
        }) { (_) -> Void in
         UIView.animateWithDuration(0.6, animations: { () -> Void in
            self.welcomeLabel.alpha = 1
            }, completion: { (_ ) -> Void in
                print("OK")
                
                //页面显示完毕后发送通知跳转主页面
                NSNotificationCenter.defaultCenter().postNotificationName(AppSwitchRootViewControler, object: nil)
})
        }
    
        
    
    
    }
    
    //MARK: 添加子视图
    private func setUpUI(){
        view.addSubview(iconImage)
        view.addSubview(welcomeLabel)
    
    //设置布局
    iconImage.snp_makeConstraints { (make) -> Void in
        make.centerX .equalTo(view.snp_centerX)
        make.bottom.equalTo(view.snp_bottom).offset(180-self.view.bounds.size.height)
        make.width.equalTo(90)
        make.height.equalTo(90)
        
    welcomeLabel.snp_makeConstraints(closure: { (make) -> Void in
        make.centerX.equalTo(view.snp_centerX)
        make.top.equalTo(iconImage.snp_bottom).offset(20)
        
    })
    
    //加载图片
      iconImage.sd_setImageWithURL(UserAccountViewModel().headURL, placeholderImage: UIImage(named: "avatar_default_big"))
    //图片设置圆角
      iconImage.layer.cornerRadius = 45
      iconImage.layer.masksToBounds = true
        
        }
    
    
    
    
    }
    //MARK: 懒加载控件
    private lazy var backImage: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    private lazy var iconImage : UIImageView = UIImageView()
    
//    private lazy var welcomeLabel : UILabel = {
//        let l = UILabel()
//        l.textAlignment = .Center
//        l.numberOfLines = 0
//        l.textColor = UIColor.lightGrayColor()
//        l.alpha = 0.5
//        l.text = (UserAccountViewModel().userName ?? "") + " 欢迎归来"
//        l.sizeToFit()
//        return l
//    }()
    private lazy var welcomeLabel : UILabel = UILabel(title: (UserAccountViewModel().userName ?? "") + " 欢迎归来", size: 14, color: UIColor.lightGrayColor())
    
    
}
